package com.wyzz.hr.exportfile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.pojo.Empsalarypay;

public class ExportSalaryToBank extends SingleSheetExportHandler {

	public ExportSalaryToBank(List result, String sheetTitle) {
		super(result, sheetTitle);
		doSort();
	}

	protected void buildTitle() throws RowsExceededException, WriteException {
	}

	private void doSort() {
		SalaryRecordSorter sorter = new SalaryRecordSorter(result);
		String props[] = { "espBankName", "espDept.departmentName",
				"espEmpno.empDistinctNo" };
		sorter.doSort(props);
	}

	private WritableSheet buildSheet(String sheetTitle, int sheetIndex)
			throws RowsExceededException, WriteException {
		WritableSheet writableSheet = writableWorkbook.createSheet(sheetTitle,
				sheetIndex);
		writableSheet.setRowView(0, 500);
		writableSheet.setColumnView(2, 15);
		writableSheet.setColumnView(3, 15);
		writableSheet.addCell(new Label(0, 0, "\u5DE5\u53F7", titleCellFormat));
		writableSheet.addCell(new Label(1, 0, "\u59D3\u540D", titleCellFormat));
		writableSheet.addCell(new Label(2, 0, "\u94F6\u884C\u5E10\u53F7",
				titleCellFormat));
		writableSheet.addCell(new Label(3, 0, "\u94F6\u884C\u5F00\u6237\u884C",
				titleCellFormat));
		writableSheet.addCell(new Label(4, 0, "\u7A0E\u540E\u6536\u5165",
				titleCellFormat));
		return writableSheet;
	}

	protected void buildBody() throws RowsExceededException, WriteException,
			IOException {
		String bankName = null;
		int size = result.size();
		int sheetIndex = 0;
		int countIndex = 0;
		for (int i = 0; i < size; i++) {
			Empsalarypay tmpEmpSP = (Empsalarypay) result.get(i);
			if (i == 0 && StringUtils.isEmpty(tmpEmpSP.getEspBankName())) {
				writableSheet = buildSheet(sheetTitle, 0);
				sheetIndex = 1;
			} else if (StringUtils.isNotEmpty(tmpEmpSP.getEspBankName())
					&& !StringUtils.equals(tmpEmpSP.getEspBankName(), bankName)) {
				writableSheet = buildSheet((new StringBuilder()).append(
						sheetTitle).append(tmpEmpSP.getEspBankName())
						.toString(), sheetIndex++);
				countIndex = 0;
			}
			bankName = tmpEmpSP.getEspBankName();
			writableSheet.setRowView(countIndex + 1, 400);
			writableSheet.addCell(new Label(0, countIndex + 1, tmpEmpSP
					.getEspEmpno().getEmpDistinctNo(), defaultCellFormat));
			writableSheet.addCell(new Label(1, countIndex + 1, tmpEmpSP
					.getEspEmpno().getEmpName(), defaultCellFormat));
			writableSheet.addCell(new Label(2, countIndex + 1, tmpEmpSP
					.getEspBankAccountNo(), defaultCellFormat));
			writableSheet.addCell(new Label(3, countIndex + 1, tmpEmpSP
					.getEspBankName()));
			BigDecimal salary = tmpEmpSP.getShowColumn19();
			Number nb = new Number(4, countIndex + 1, salary.floatValue(),
					numberCellFormat);
			writableSheet.addCell(nb);
			countIndex++;
		}

		writableWorkbook.write();
	}
}
